<template>
  <div class="articles-add">
    <el-dialog :title="showTitle" :visible="showAddDialog" @close="btnCancel">
    <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px">
      <el-form-item label="文章标题：" prop="title">
        <el-input v-model="addForm.title" placeholder="请输入文章标签" size="small"></el-input>
      </el-form-item>
      <el-form-item label="文章内容：" prop="articleBody" >
        <quill-editor v-model="addForm.articleBody" ref="myQuillEditor" class="editor" :options="editorOption" />
      </el-form-item>
      <el-form-item label="视频地址：">
        <el-input v-model="addForm.videoURL" placeholder="请输入视频地址" size="small"></el-input>
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="end">
        <el-col :span="6">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="primary" @click="btnOK">确认</el-button>
        </el-col>
      </el-row>
  </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { add, update } from '@/api/hmmm/articles'
// 工具栏配置
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote', 'code-block'], // 引用  代码块-----['blockquote', 'code-block']
  ['image'] // 链接、图片、视频-----['link', 'image', 'video']
]
export default {
  components: { quillEditor },
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    },
    addForm: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    showTitle () {
      return this.addForm.id ? '修改文章' : '新增文章'
    }
  },
  data () {
    return {
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      // 富文本属性
      editorOption: {
        placeholder: '',
        theme: 'snow',
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      }
    }
  },
  methods: {
    async btnOK () {
      try {
        await this.$refs.addForm.validate()
        if (this.addForm.id) {
          await update(this.addForm)
        } else {
          await add(this.addForm)
        }
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.$parent.showAddDialog = false
        this.$emit('getList')
        this.$parent.editData.id = ''
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel () {
      this.$emit('update:showAddDialog', false)
      this.$parent.editData.id = ''
      this.$parent.editData = {}
      this.$refs.addForm.resetFields() // 重置校验结果
    }
  }
}
</script>

<style scoped lang='scss'>
.articles-add ::v-deep .ql-editor{
  height: 200px;
}
.articles-add ::v-deep .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
